package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.MemberPackageConfig;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.PCLogin;
import com.ruoyi.system.domain.vo.PCLoginVo;
import com.ruoyi.system.domain.vo.PcLoginSearchVo;
import com.ruoyi.system.dto.PcLoginListDTO;
import com.ruoyi.system.mapper.OrderMapper;
import com.ruoyi.system.mapper.PCMapper;
import com.ruoyi.system.service.IPCService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class PCServiceImpl extends ServiceImpl<PCMapper, PCLogin> implements IPCService {

    @Override
    public Boolean pcLoginCheck(String phone) {
        return baseMapper.getPCLoginByPhone(phone)!=0;
    }

    @Override
    public List<PcLoginListDTO> selectPcLoginList(PcLoginSearchVo vo) {
        List<PcLoginListDTO> pcLoginListDTOS = baseMapper.selectPcLoginList(vo);
        for (PcLoginListDTO pcLoginListDTO : pcLoginListDTOS) {
            if (StringUtils.isBlank(pcLoginListDTO.getEndTime())){
                pcLoginListDTO.setState(1);
            }else {
                LocalDate now = LocalDate.now();
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                if (now.isBefore(LocalDate.parse(pcLoginListDTO.getEndTime(), fmt))){
                    pcLoginListDTO.setState(0);
                }
            }

        }
        return pcLoginListDTOS;
    }

    @Override
    public Boolean addPcLogin(PCLoginVo loginVo) {
        PCLogin pcLogin = new PCLogin();
        BeanUtils.copyProperties(loginVo, pcLogin);
        int result = baseMapper.insertPcLogin(pcLogin);
        if (result > 0) {
            return true;
        }
        return null;
    }

    @Override
    public Boolean del(Integer id) {
        return baseMapper.deleteById(id)>0;
    }

    @Override
    public int countList(PcLoginSearchVo searchVo) {

        return Math.toIntExact(baseMapper.selectCount(
            new LambdaQueryWrapper<PCLogin>()
                .eq(searchVo.getState() != null, PCLogin::getStatus, searchVo.getState())
                .and(searchVo.getPhone() != null && !searchVo.getPhone().isEmpty(), w1 -> w1.like(PCLogin::getUserPhone, searchVo.getPhone()))
                .ge(searchVo.getBeginTime() != null && !searchVo.getBeginTime().isEmpty(), PCLogin::getStartTime, searchVo.getBeginTime())
                .le(searchVo.getEndTime() != null && !searchVo.getEndTime().isEmpty(), PCLogin::getEndTime, searchVo.getBeginTime())));
    }
}
